package cc.messcat.service.fupin;

import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.dao.fupin.VisionDao;
import cc.messcat.entity.fupin.Vision;
import cc.messcat.util.Collections;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2018/4/27
 */
@Service
public class VisionServiceImpl extends BaseServiceImpl<Vision> implements VisionService {
    @Autowired
    private VisionDao visionDao;

    @Override
    public Long merge(Vision vision) {
        return visionDao.merge(vision);
    }

    /**
     * 根据所属村id和上传日期获取街景
     */
    @Override
    public List<Vision> listVision(Long id, String date, Integer pageNo, Integer pageSize) {

        if(pageNo == null || pageSize == null) {
            String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') =:date " : "";
            String hql = " from Vision where villageID=:id "+ dateCondition +" order by createTime desc ";
            return visionDao.queryList(hql,Collections.<String,Long>generateHashMap("id",id,"date",date));
        }

        String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') =? " : "";
        String hql = " from Vision where villageID=? "+ dateCondition +" order by createTime desc ";
        return StringUtils.isNotBlank(date) ? visionDao.findPageObjects(hql,pageNo,pageSize,id,date) :  visionDao.findPageObjects(hql,pageNo,pageSize,id);
    }

    @Override
    public int count(Long villageID, String date) {
        String dateCondition  = StringUtils.isNotBlank(date) ? " and DATE_FORMAT(createTime,'%Y-%m-%d') = '"+date+"'" : "";
        return ((Long)visionDao.find("select count(*) from Vision where  villageID="+villageID + dateCondition).get(0)).intValue();
    }

    @Override
    public List<String> listVisionDateByVillageID(Long villageID ) {
        String hql = "select distinct(DATE_FORMAT(createTime,'%Y-%m-%d')) from Vision where villageID=:id order by createTime desc ";
        return visionDao.queryList(hql,Collections.<String,Long>generateHashMap("id",villageID));
    }

}
